package com.xw.qxypt.modules.util;


import com.xw.qxypt.modules.entity.*;
import java.math.BigDecimal;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.*;


public class MessageTranslatorUtil {

    //路赋德二进制数据解析存储
    public static void saveLUFFTBinaryValue(String mes,HashMap<String, DataCollection> map,String sensorNo) {
        //SetLogFileName(sensorNo);
        //logger.error(DateConvertUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss")+"  "+sensorNo+"  接收到  "+mes);
        String str = mes;
        // 截取<cs>字段
        String cs = str.substring(str.length() - 6, str.length() - 2);
        // CRC校验 以保证数据传输的正确性和完整性
        String crc = XwSocketUtil.crc_16_CCITT(str.substring(0, str.length() - 6));
        // 字节换位，如：666696C1->C1966666 --start
        String[] crcArray = new String[crc.length() / 2];
        for (int i = 0; i < crcArray.length; i++) {
            crcArray[i] = crc.substring(i * 2, i * 2 + 2);
        }
        crc = "";
        for (int i = crcArray.length - 1; i >= 0; i--) {
            crc += crcArray[i];
        }
        //System.out.println("<cs1> = " + cs);
        //System.out.println("soh2EtxCRC = " + crc);
        // 判断校验码是否一致
        if (crc.equals(cs)) {
            // 截取<from>字段
            String from = str.substring(8, 12);
            //System.out.println("from--"+from);
            if(map.containsKey(sensorNo) && map.get(sensorNo).getEquipmentMap().containsKey(from)){

                DataCollection dc = map.get(sensorNo);
                //System.out.println(map.get(sensorNo).getLemMap().get(from));
                LhpEquipmentEntity lem = (LhpEquipmentEntity) map.get(sensorNo).getEquipmentMap().get(from);



                LhpDataRoadDetectorEntity ldrd = new LhpDataRoadDetectorEntity();
                LhpDataVisibilityEntity ldv = new LhpDataVisibilityEntity();
                LhpDataWeatherStationEntity ldws = new LhpDataWeatherStationEntity();


                Date currentTime = new Date();
                SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String dateString = formatter.format(currentTime);


                ldrd.setPointId(lem.getPointId());
                ldrd.setEquipmentId(lem.getId());
                ldrd.setDataTime(currentTime);

                ldv.setPointId(lem.getPointId());
                ldv.setEquipmentId(lem.getId());
                ldv.setDataTime(currentTime);

                ldws.setPointId(lem.getPointId());
                ldws.setEquipmentId(lem.getId());
                ldws.setDataTime(currentTime);

                // 截取<len>字段
                String len = str.substring(12, 14);
                // 截取<cmd>字段
                String cmd = str.substring(16, 18);
                // 截取<status>字段
                String status = str.substring(20, 22);// 状态 0 表示正常 其他表示错误
                //数据状态正常，则进行存储
                if(status.equals("00")){

                    //System.out.println("<len> = " + len);
                    //System.out.println("<cmd> = " + cmd);
                    //System.out.println("<status> = " + status);
                    // 截取<channel>字段
                    String channel;
                    // 截取<type>字段 数据类型
                    String type;
                    // 截取<value>字段
                    String value = "";
                    if (cmd.equals("23")) {// 单通道解析
                        // 截取<channel>字段
                        channel = str.substring(22, 26);
                        // 截取<type>字段 数据类型
                        type = str.substring(26, 28);
                        // 截取<value>字段
                        int endIdex = (Integer.parseInt(len, 16) - 6) * 2;
                        value = str.substring(28, 28 + endIdex);
                        //System.out.println("单通道-<channel> = " + channel);
                        //System.out.println("单通道-<type> = " + type);
                        //System.out.println("单通道-<value> = " + value);
                        // 计算value值
                        value = XwSocketUtil.calValueByType(type, value);

                        //能见度
                        if(from.equals("0130")){
                            //System.out.println(sensorNo+"--"+mes);
                            switch(channel){

                                //能见度
                                case "5902":

                                    ldv.setVisibility(value);
                                    break;
                            }
                        }
                        //被动式路面探测器
                        else if(from.equals("0190")){

                            switch(channel){

                                //路面温度
                                case "6500":

                                    ldrd.setRoadSurfaceTemp(value);
                                    break;
                                //摩擦系数
                                case "3403":

                                    ldrd.setFriction(value);
                                    break;
                                //冰点温度 NaCl
                                case "9700":
                                    ldrd.setFreezingTempNacl(value);
                                    break;
                                //冰点温度 MgCl
                                case "AB00":
                                    ldrd.setFreezingTempMgcl(value);
                                    break;
                                //冰点温度 CaCl
                                case "BF00":
                                    ldrd.setFreezingTempCacl(value);
                                    break;
                                //水膜高度
                                case "5902":
                                    ldrd.setWaterFilmHeight(value);
                                    break;
                                //含盐量 NaCl
                                case "2103":
                                    ldrd.setSalineConcentrationNacl(value);
                                    break;
                                //含冰比例
                                case "2A03":
                                    ldrd.setIcePercentage(value);
                                    break;
                                //含盐量 MgCl
                                case "3F03":
                                    ldrd.setSalineConcentrationMgcl(value);
                                    break;
                                //含盐量 CaCl
                                case "4903":
                                    ldrd.setSalineConcentrationCacl(value);
                                    break;
                                //路面状态
                                case "8403":
                                    ldrd.setRoadCondition(value);
                                    break;
                                default:
                                    break;
                            }
                        }

//						System.out.println("单通道-<value> = " + value);
                        // 依据<channel>将value存入响应的字段，此处还没完成。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
                        // 多通道解析
                    } else if (cmd.equals("2F")) {
                        String num = str.substring(22, 24);
                        String payload = str.substring(24, 24 + (Integer.parseInt(
                                len, 16) - 4) * 2);
                        //System.out.println("<payload> = " + payload);
                        // 循环截取每个通道：{<长度>，<状态>，<通道>,<类型>,<值>}
                        int beginIndex = 0;
                        int endIndex = 0;
                        String channelStr;// {<长度>，<状态>，<通道>,<类型>,<值>}
                        String lenD;// 每个通道的长度
                        for (int i = 0; i < Integer.parseInt(num, 16); i++) {
                            lenD = payload.substring(beginIndex, beginIndex + 2);
                            endIndex = endIndex + 2 + Integer.parseInt(lenD) * 2;
                            //System.out.println("lenD = " + lenD);
                            //System.out.println("beginIdex = " + beginIndex);
                            //System.out.println("endIndex = " + endIndex);
                            // {<长度>，<状态>，<通道>,<类型>,<值>}

                            channelStr = payload.substring(beginIndex, endIndex);
                            //System.out.println("<channelStr>-" + i + "- = " + channelStr);
                            // 截取<channel>字段
                            channel = channelStr.substring(4, 8);

                            if(!lenD.equals("03")){

                                //System.out.println("<channel>-" + i + "- = " + channel);
                                // 截取<type>字段 数据类型
                                type = channelStr.substring(8, 10);
                                //System.out.println("<type>-" + i + "- = " + type);
                                // 截取<value>字段
                                value = channelStr.substring(10);
                                //System.out.println("<value>-" + i + "- = " + value);
                                value = XwSocketUtil.calValueByType(type, value);
                                //System.out.println("<value>-" + i + "- = " + value);
                                // 依据<channel>将value存入响应的字段，此处还没完成。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
                                beginIndex = beginIndex + Integer.parseInt(lenD) * 2 + 2;

                            }else{

                                beginIndex = beginIndex + Integer.parseInt(lenD) * 2 + 2;

                                continue;
                            }

                            //能见度
                            if(from.equals("0130")){

                                switch(channel){

                                    //能见度
                                    case "5902":

                                        ldv.setVisibility(value);
                                        break;
                                }
                            }


                            //被动式路面探测器
                            else if(from.equals("0190")){

                                switch(channel){

                                    //路面温度
                                    case "6500":
                                        ldrd.setRoadSurfaceTemp(value);
                                        break;
                                    //摩擦系数
                                    case "3403":

                                        ldrd.setFriction(value);
                                        break;
                                    //冰点温度 NaCl
                                    case "9700":
                                        ldrd.setFreezingTempNacl(value);
                                        break;
                                    //冰点温度 MgCl
                                    case "AB00":
                                        ldrd.setFreezingTempMgcl(value);
                                        break;
                                    //冰点温度 CaCl
                                    case "BF00":
                                        ldrd.setFreezingTempCacl(value);
                                        break;
                                    //水膜高度
                                    case "5902":
                                        ldrd.setWaterFilmHeight(value);
                                        break;
                                    //含盐量 NaCl
                                    case "2103":
                                        ldrd.setSalineConcentrationNacl(value);
                                        break;
                                    //含冰比例
                                    case "2A03":
                                        ldrd.setIcePercentage(value);
                                        break;
                                    //含盐量 MgCl
                                    case "3F03":
                                        ldrd.setSalineConcentrationMgcl(value);
                                        break;
                                    //含盐量 CaCl
                                    case "4903":
                                        ldrd.setSalineConcentrationCacl(value);
                                        break;
                                    //路面状态
                                    case "8403":
                                        ldrd.setRoadCondition(value);
                                        break;

                                    default:
                                        break;
                                }
                            }
                        }
                    }
                    //能见度
                    if(from.equals("0130")){

                        dc.setLhpDataVisibilityEntity(ldv);
                    }
                    else{

                        dc.setLhpDataRoadDetectorEntity(ldrd);
                    }

                }else{

                    //能见度
                    if(from.equals("0130")){

                        dc.setLhpDataVisibilityEntity(null);
                    }
                    else{

                        dc.setLhpDataRoadDetectorEntity(null);
                    }
                }

            }

        }
        else{

            //System.out.println(mes);
        }
    }
    public static void saveVaisalaDate(String mes,HashMap<String, DataCollection> map,String sensorNo) {
        if (map.containsKey(sensorNo) && map.get(sensorNo).getEquipmentMap().size() > 0) {

            DataCollection dc = map.get(sensorNo);
            Map<String, LhpEquipmentEntity> map1 = map.get(sensorNo).getEquipmentMap();
            LhpEquipmentEntity lem = new LhpEquipmentEntity();
            for (Map.Entry<String, LhpEquipmentEntity> entry : map1.entrySet()) {
                lem = entry.getValue();
            }
            LhpDataRoadDetectorEntity ldrd = new LhpDataRoadDetectorEntity();
            LhpDataVisibilityEntity ldv = new LhpDataVisibilityEntity();
            LhpDataWeatherStationEntity ldws = new LhpDataWeatherStationEntity();


            Date currentTime = new Date();
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String dateString = formatter.format(currentTime);


            ldrd.setPointId(lem.getPointId());
            ldrd.setEquipmentId(lem.getId());
            ldrd.setDataTime(currentTime);

            ldv.setPointId(lem.getPointId());
            ldv.setEquipmentId(lem.getId());
            ldv.setDataTime(currentTime);

            ldws.setPointId(lem.getPointId());
            ldws.setEquipmentId(lem.getId());
            ldws.setDataTime(currentTime);

            String info = mes.split("DSC")[1];
            String[] infos = info.split(";");
            Map<String,String> infoMap = new HashMap<>();
            for(int i=0;i<infos.length-1;i++){
                String name = infos[i].substring(0,2);
                String value = infos[i].substring(2).trim();
                infoMap.put(name,value);
            }
            //01 气温 02相对湿度  03露点  05 风速 06风向 08降水总量 12大气压力    09降雨强度 10积雪高度
            // 23 降水类型（0.无降水1小雨2中雨3大雨4小雪5中雪6大雪）26.10分钟内最强风速
            //11 能见度
            //66路面状态--只取最后一位（0err1干2潮3湿6雪7冰9冰水混合） 68湿滑程度  72水积累量 73 冰积累量 74 雪积累量
            if(infoMap.containsKey("11")&& infoMap.get("11").indexOf("/")==-1 && infoMap.get("11").indexOf("#")==-1){
                ldv.setVisibility(infoMap.get("11"));
            }
            if(infoMap.containsKey("01")&& infoMap.get("01").indexOf("/")==-1 && infoMap.get("01").indexOf("#")==-1){
                ldws.setAirTemp(infoMap.get("01"));
            }
            if(infoMap.containsKey("02")&& infoMap.get("02").indexOf("/")==-1 && infoMap.get("02").indexOf("#")==-1){
                ldws.setRelativeHumidity(infoMap.get("02"));
            }
            if(infoMap.containsKey("03")&& infoMap.get("03").indexOf("/")==-1 && infoMap.get("03").indexOf("#")==-1){
                ldws.setDewPointTemp(infoMap.get("03"));
            }
            if(infoMap.containsKey("05")&& infoMap.get("05").indexOf("/")==-1 && infoMap.get("05").indexOf("#")==-1){
                ldws.setWindSpeed(infoMap.get("05"));
            }
            if(infoMap.containsKey("06")&& infoMap.get("06").indexOf("/")==-1 && infoMap.get("06").indexOf("#")==-1){
                ldws.setWindDirection(infoMap.get("06"));
            }
            if(infoMap.containsKey("08")&& infoMap.get("08").indexOf("/")==-1 && infoMap.get("08").indexOf("#")==-1){
                ldws.setPrecipitation(infoMap.get("08"));
            }
            if(infoMap.containsKey("09")&& infoMap.get("09").indexOf("/")==-1 && infoMap.get("09").indexOf("#")==-1){
                ldws.setPrecipitationIntensity(infoMap.get("09"));
            }
            if(infoMap.containsKey("10")&& infoMap.get("10").indexOf("/")==-1 && infoMap.get("10").indexOf("#")==-1){
                ldws.setSnowAmount(infoMap.get("10"));
            }
            if(infoMap.containsKey("12")&& infoMap.get("12").indexOf("/")==-1 && infoMap.get("12").indexOf("#")==-1){
                ldws.setPressure(infoMap.get("12"));
            }
            if(infoMap.containsKey("23")&& infoMap.get("23").indexOf("/")==-1 && infoMap.get("23").indexOf("#")==-1){
                ldws.setPrecipitationType(infoMap.get("23"));
            }
            if(infoMap.containsKey("26")&& infoMap.get("26").indexOf("/")==-1 && infoMap.get("26").indexOf("#")==-1){
                ldws.setWindSpeedMax10(infoMap.get("26"));
            }
            if(infoMap.containsKey("66")&& infoMap.get("66").indexOf("/")==-1 && infoMap.get("66").indexOf("#")==-1){
                String status = infoMap.get("66").substring(infoMap.get("66").length()-1);
                ldrd.setRoadCondition(status);
            }
            if(infoMap.containsKey("68")&& infoMap.get("68").indexOf("/")==-1 && infoMap.get("68").indexOf("#")==-1){
                ldrd.setFriction(infoMap.get("68"));
            }
            if(infoMap.containsKey("72")&& infoMap.get("72").indexOf("/")==-1 && infoMap.get("72").indexOf("#")==-1){
                ldrd.setWaterAmount(infoMap.get("72"));
            }
            if(infoMap.containsKey("73")&& infoMap.get("73").indexOf("/")==-1 && infoMap.get("73").indexOf("#")==-1){
                ldrd.setIceAmount(infoMap.get("73"));
            }
            if(infoMap.containsKey("74")&& infoMap.get("74").indexOf("/")==-1 && infoMap.get("74").indexOf("#")==-1){
                ldrd.setSnowAmount(infoMap.get("74"));
            }
            dc.setLhpDataVisibilityEntity(ldv);
            dc.setLhpDataRoadDetectorEntity(ldrd);
            dc.setLhpDataWeatherStationEntity(ldws);
        }
    }
    public static void saveVaisalaDatePWD(List<String> mes, HashMap<String, DataCollection> map, String sensorNo) {
        if (map.containsKey(sensorNo) && map.get(sensorNo).getEquipmentMap().size() > 0) {
            DataCollection dc = map.get(sensorNo);
            Map<String, LhpEquipmentEntity> map1 = map.get(sensorNo).getEquipmentMap();
            LhpEquipmentEntity lem = new LhpEquipmentEntity();
            for (Map.Entry<String, LhpEquipmentEntity> entry : map1.entrySet()) {
                lem = entry.getValue();
            }
            LhpDataVisibilityEntity ldv = new LhpDataVisibilityEntity();


            Date currentTime = new Date();
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String dateString = formatter.format(currentTime);

            ldv.setPointId(lem.getPointId());
            ldv.setEquipmentId(lem.getId());
            ldv.setDataTime(currentTime);

            String visibility = mes.get(2);
            if( visibility.indexOf("/")==-1 && visibility.indexOf("#")==-1){
                ldv.setVisibility(visibility);
            }
            dc.setLhpDataVisibilityEntity(ldv);
        }
    }
}
